package com.yvdelx.cyclowomen.activity;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.object.Cycle;
import com.yvdelx.cyclowomen.utility.DateUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;
import com.yvdelx.cyclowomen.utility.ViewUtility;

import java.util.List;

/**
 * Dialog to choose a cycle
 * Created by ydelvaux on 26/06/2014.
 */
public class CycleChoiceDialog extends Dialog implements View.OnClickListener{


    private final List<Cycle> cycles;
    private final Activity activity;
    private Cycle cycle;
    private Boolean onBackSelected = false;

    @SuppressWarnings("ConstantConditions")
    public CycleChoiceDialog(List<Cycle> cycles, Activity activity) {
        super(activity);
        this.cycles = cycles;
        this.activity = activity;
    }

    public Cycle getCycle(){
        return this.cycle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_cycle_choice);
        this.setTitle(R.string.cycle_choice_title);

        // Instantiate the radio group view
        RadioGroup radioGroup = ViewUtility.getRadioGroup(this);

        // Instantiate the radios buttons of the radio group
        RadioButton radioButton;
        String text;
        for(Cycle cycle : cycles){
            radioButton = new RadioButton(this.getContext());
            text = StringUtility.getStringResource(R.string.cycle,this.activity) + " "
                    + StringUtility.getStringResource(R.string.du,this.activity) + " "
                    + DateUtility.dateToString(cycle.getDateDebut()) + " "
                    + StringUtility.getStringResource(R.string.au,this.activity) + " "
                    + DateUtility.dateToString(cycle.getDateFin());
            radioButton.setId(Integer.valueOf(cycle.getIdCycle()));
            radioButton.setText(text);
            radioButton.setOnClickListener(this);
            radioGroup.addView(radioButton);
        }

    }

    @Override
    public void onClick(View view) {
        // Get the id object selected
        Integer idView = view.getId();

        // Find the cycle with the id in the cycle list displayed at the user (max 3 cycles)
        for(Cycle cl : this.cycles){
            if(idView.equals(Integer.valueOf(cl.getIdCycle()))){
                this.cycle = cl;
                break;
            }
        }
        this.dismiss();
    }

    @Override
    public void onBackPressed() {
        this.onBackSelected = true;
        super.onBackPressed();
    }

    /**
     * Return true if the dismiss has been called by the back button
     * @return Boolean
     */
    public Boolean isOnBackSelected(){
        return this.onBackSelected;
    }
}
